package org.vgou.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.directwebremoting.annotations.Param;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.vgou.dao.IBaseDao;
import org.vgou.model.Business;
import org.vgou.service.IBusinessService;

/**
 * 
 * Copyright (c) 2011, vgou <br>
 * All rights reserved.<br>
 * 
 * @since 2011-11-27
 * @author <a href="mailto:jason.hxx@gmail.com">jason</a>
 * @version Id: BusinessServiceImpl.java
 * @comment 商家服务类
 * 
 */
@Component("businessService")
@RemoteProxy(name = "BusinessRemote", creator = SpringCreator.class, creatorParams = { @Param(name = "beanName", value = "BusinessServiceImpl") })
public class BusinessServiceImpl implements IBusinessService {

	protected final Log logger = LogFactory.getLog(getClass());
	@Resource
	private IBaseDao baseDao;

	/**
	 * 创建商家记录
	 * 
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public long create(Business business) throws Exception {
		long id = 0;
		if (business == null)
			return id;
		try {
			id = (Long) baseDao.create(business);
		} catch (Exception e) {
			logger.error("创建商家失败：" + e.getMessage());
		}
		return id;
	}

	/**
	 * 修改商家记录
	 * 
	 * @param business
	 * @return
	 * @throws Exception
	 */
	public int update(Business business) throws Exception {
		int result = -1;
		if (business == null)
			return result;
		try {
			baseDao.update(business);
			result = 1;
		} catch (Exception e) {
			logger.error("修改商家记录失败：" + e.getMessage());
		}
		return result;
	}

	/**
	 * 删除商家记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RemoteMethod
	public int del(long id) throws Exception {
		int result = -1;
		if (id <= 0)
			return result;
		try {
			Business business = getById(id);
			if (business != null)
				baseDao.delete(business);
			result = 1;
		} catch (Exception e) {
			logger.error("删除商家记录失败：" + e.getMessage());
		}
		return result;
	}
	
	/**
	 * 批量删除商家记录
	 * @param idsStr
	 * @return
	 * @throws Exception
	 */
	@RemoteMethod
	public int batchDel(String idsStr) throws Exception{
		int result = -1;
		String[] ids = idsStr.split(",");
		if(ids.length == 0)
			return result;
		try {
			for(int i=0; i<ids.length; i++){
				long id = Long.valueOf(ids[i]);
				Business business = getById(id);
				if (business != null)
					baseDao.delete(business);
			}
			result = 1;
		} catch (Exception e) {
			logger.error("批量删除商家记录失败：" + e.getMessage());
		}
		return result;
	}

	/**
	 * 修改商家状态
	 * 
	 * @param id
	 * @param status
	 *            修改后的状态
	 * @return
	 * @throws Exception
	 */
	@RemoteMethod
	public int updateStatus(long id, int status) throws Exception {
		int result = -1;
		if (id <= 0)
			return result;
		try {
			Business business = getById(id);
			if (business != null) {
				business.setStatus(status);
				baseDao.update(business);
			}
			result = 1;
		} catch (Exception e) {
			logger.error("修改商家状态失败：" + e.getMessage());
		}
		return result;
	}
	
	/**
	 * 批量删除商家记录
	 * @param idsStr
	 * @return
	 * @throws Exception
	 */
	@RemoteMethod
	public int batchUpdateStatus(String idsStr, int status) throws Exception{
		int result = -1;
		String[] ids = idsStr.split(",");
		if(ids.length == 0)
			return result;
		try {
			for(int i=0; i<ids.length; i++){
				long id = Long.valueOf(ids[i]);
				Business business = getById(id);
				if (business != null)
					business.setStatus(status);
					baseDao.update(business);
			}
			result = 1;
		} catch (Exception e) {
			logger.error("批量修改商家状态失败：" + e.getMessage());
		}
		return result;
	}

	/**
	 * 根据商家ID取得商家记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public Business getById(long id) throws Exception {
		Business business = null;
		if (id <= 0)
			return business;
		try {
			business = (Business) baseDao.getByPk(Business.class, id);
		} catch (Exception e) {
			logger.error("根据商家ID取得商家记录失败：" + e.getMessage());
		}
		return business;
	}

	/**
	 * 取得指定条件下的商家数目
	 * 
	 * @param businessName
	 *            商家名称
	 * @param status
	 *            状态
	 * @return
	 * @throws Exception
	 */
	public int getAllCount(String businessName, int status) throws Exception {
		int count = 0;
		Criteria c = baseDao.getCriteria(Business.class);
		if (businessName != null && !"".equals(businessName)) {
			c.add(Restrictions.eq("businessName", businessName));
		}
		if (status > 0) {
			c.add(Restrictions.eq("status", status));
		}
		c.setProjection(Projections.rowCount());
		count = ((Number) c.uniqueResult()).intValue();
		return count;
	}

	/**
	 * 取得指定条件下的商家集合
	 * 
	 * @param businessName
	 *            商家名称
	 * @param status
	 *            状态
	 * @param orderBy
	 *            排序属性
	 * @param isAsc
	 *            是否升序
	 * @param startPage
	 *            分页开始索引
	 * @param pageSize
	 *            每页显示数目
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Business> getAll(String businessName, int status,
			String orderBy, boolean isAsc, int startPage, int pageSize)
			throws Exception {
		List<Business> businessList = null;
		try {
			DetachedCriteria c = DetachedCriteria.forClass(Business.class);
			if (businessName != null && !"".equals(businessName)) {
				c.add(Restrictions.eq("businessName", businessName));
			}
			if (status > 0) {
				c.add(Restrictions.eq("status", status));
			}
			orderBy = (orderBy == null || "".equals(orderBy)) ? "id" : orderBy;
			if (isAsc) {
				c.addOrder(Order.asc(orderBy));
			} else {
				c.addOrder(Order.desc(orderBy));
			}
			businessList = baseDao.findByCriteria(c, startPage, pageSize);
		} catch (Exception e) {
			logger.error("取得指定条件下的商家集合失败：" + e.getMessage());
		}
		return businessList;
	}

}
